package thread;

import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * 线程池测试类
 */
public class ThreadPoolTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        /**
         *  线程池任务处理
         */
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        ArrayList<Future<String>> futures = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            futures.add(cachedThreadPool.submit(new LuckyDraw()));
        }
        for (Future<String> future :
                futures) {
            if(!future.get().equals("未中奖")){
                System.out.print(future.get()+"，");
            }
        }


        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
        for (int i = 0; i < 100; i++) {
            scheduledExecutorService.schedule(new LuckyDraw(),10L,TimeUnit.SECONDS);
        }


    }


}

class LuckyDraw implements Callable<String> {

    @Override
    public String call() throws Exception {
        String prize = "未中奖";
        int i = ThreadLocalRandom.current().nextInt(100);
        if (i > 30) {
            prize = "中奖" + i;
        }
        System.out.println(Thread.currentThread().getName()+"执行抽奖"+prize);
        return prize;
    }
}

